package com.yingpai.fitness.dvp_2_0.main.activity.dynamic;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.base.BaseResponse;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.dynamic.DynamicListBean;
import com.yingpai.fitness.entity.dynamic.FansBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ASUS on 2018/5/16.
 */

public class DynamicListActivity extends CommonActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private String flag;
    private int pageNum = 1;
    private int pageSize = 10;
    private QuickAdapter<FansBean.MapBean.CustomerListBean> fansAdapter;
    private QuickAdapter<DynamicListBean.MapBean.PageInfoBean.ListBean> dynamicAdapter;
    private int id;
    private TokenBean tokenBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_dynamic_list);

    }

    @Override
    protected void initView() {
        refreshLayout.setOnRefreshLoadMoreListener(this);

        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            flag = bundle.getString("flag");
            id = bundle.getInt("id", -1);
        }

        switch (flag) {
            case "follow":
                setTitle("关注");
                fansAdapter = new QuickAdapter<FansBean.MapBean.CustomerListBean>(this, R.layout.my_fan_or_follow_list_item) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, final FansBean.MapBean.CustomerListBean item) {
                        helper.setText(R.id.fan_or_follow_user_name, item.getNickName());
                        CircleImageView imageView = helper.getView(R.id.fan_or_follow_user_head_iv);
                        Glide.with(context)
                                .load(item.getPhotoUrl())
                                .placeholder(R.mipmap.icon_user_default)
                                .centerCrop()
                                .into(imageView);
                        final TextView button = helper.getView(R.id.fan_or_follow_btn);
                        if (item.isFollowed()) {
                            helper.setText(R.id.fan_or_follow_btn, "已关注");
                        } else {
                            helper.setText(R.id.fan_or_follow_btn, "关注");
                        }

                        button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                HttpParams params = new HttpParams();
                                params.put("myId", tokenBean.getId());
                                params.put("otherId", item.getId());
                                finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.FOLLOW_OR_UNFOLLOW, params, Url.FOLLOW_OR_UNFOLLOW, new Callback<BaseResponse>() {
                                    @Override
                                    public void onSuccess(BaseResponse baseResponse) {
                                        if (Constant.REQUEST_SUCCESS.equals(baseResponse.getResult())) {
                                            if (item.isFollowed()) {
                                                item.setFollowed(false);
                                                button.setText("关注");
                                            } else {
                                                button.setText("已关注");
                                                item.setFollowed(true);
                                            }
                                        }
                                    }
                                });
                            }
                        });

                    }
                };
                listview.setAdapter(fansAdapter);
                break;
            case "fans":
                setTitle("粉丝");
                fansAdapter = new QuickAdapter<FansBean.MapBean.CustomerListBean>(this, R.layout.my_fan_or_follow_list_item) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, final FansBean.MapBean.CustomerListBean item) {
                        helper.setText(R.id.fan_or_follow_user_name, item.getNickName());
                        CircleImageView imageView = helper.getView(R.id.fan_or_follow_user_head_iv);
                        Glide.with(context)
                                .load(item.getPhotoUrl())
                                .placeholder(R.mipmap.icon_user_default)
                                .centerCrop()
                                .into(imageView);
                        final TextView button = helper.getView(R.id.fan_or_follow_btn);
                        if (item.isFollowed()) {
                            helper.setText(R.id.fan_or_follow_btn, "已关注");
                        } else {
                            helper.setText(R.id.fan_or_follow_btn, "关注");
                        }

                        button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                HttpParams params = new HttpParams();
                                params.put("myId", tokenBean.getId());
                                params.put("otherId", item.getId());
                                finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.FOLLOW_OR_UNFOLLOW, params, Url.FOLLOW_OR_UNFOLLOW, new Callback<BaseResponse>() {
                                    @Override
                                    public void onSuccess(BaseResponse baseResponse) {
                                        if (Constant.REQUEST_SUCCESS.equals(baseResponse.getResult())) {
                                            if (item.isFollowed()) {
                                                item.setFollowed(false);
                                                button.setText("关注");
                                            } else {
                                                button.setText("已关注");
                                                item.setFollowed(true);
                                            }
                                        }
                                    }
                                });
                            }
                        });

                    }
                };
                listview.setAdapter(fansAdapter);
                break;
            case "dynamic":
                setTitle("动态");
                dynamicAdapter = new QuickAdapter<DynamicListBean.MapBean.PageInfoBean.ListBean>(this, R.layout.dynamic_followed_list_item) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, DynamicListBean.MapBean.PageInfoBean.ListBean item) {

                        helper.setText(R.id.dynamic_followed_user_name, item.getPublisherName());
                        helper.setText(R.id.dynamic_follow_date_tv, item.getPublishTime() + "");
                        helper.setText(R.id.follow_brief_tv_content, item.getThemeTitle());
                        helper.setText(R.id.followed_user_location, item.getPublishLocale());
                        helper.setText(R.id.followed_user_collect_tv, item.getPraiseNo() + "");

                        CircleImageView imageView = helper.getView(R.id.dynamic_followed_user_head_iv);
                        Glide.with(context)
                                .load(item.getPublisherPhotoUrl())
                                .error(R.mipmap.icon_user_default)
                                .centerCrop()
                                .into(imageView);
                        ImageView photo = helper.getView(R.id.follow_brief_iv_content);
                        Glide.with(context)
                                .load(item.getPhotoUrl())
                                .error(R.mipmap.default_big_bg)
                                .into(photo);

                        ImageView follow_user_like_iv = helper.getView(R.id.follow_user_like_iv);
                        if ((Object) item.getIsPraised() != null) {
                            if (item.getIsPraised() == 0) {
                                follow_user_like_iv.setImageResource(R.mipmap.bbs_like_no);
                            } else {
                                follow_user_like_iv.setImageResource(R.mipmap.bbs_like);
                            }
                        } else {
                            follow_user_like_iv.setImageResource(R.mipmap.bbs_like_no);
                        }

                    }
                };
                listview.setAdapter(dynamicAdapter);

                listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if ((Object) dynamicAdapter.getItem(position).getIsPraised() != null) {
                            Bundle mBundle = new Bundle();
                            mBundle.putInt("id", dynamicAdapter.getItem(position).getId());
                            mBundle.putInt("isPraised", dynamicAdapter.getItem(position).getIsPraised());
                            startActivity(DynamicDetailActivity.class, mBundle);
                        }

                    }
                });
                break;
        }
    }

    @Override
    protected void initData() {
        pageNum = 1;
        switch (flag) {
            case "follow":
                requestFollowData();
                break;
            case "fans":
                requestFasData();
                break;
            case "dynamic":
                resquestData();
                break;
        }
    }


    private void requestFasData() {
        showDialog(this, null);
        HttpParams params = new HttpParams();
        params.put("myId", tokenBean.getId());
        params.put("customerId", id);
        params.put("pageNum", pageNum++);
        params.put("pageSize", pageSize);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_MY_FANS, params, Url.GET_MY_FANS, new Callback<FansBean>() {
            @Override
            public void onSuccess(FansBean fansBean) {
                if (fansBean.getResult().equals(Constant.REQUEST_SUCCESS)) {
                    if (fansBean.getMap() != null) {
                        refreshLayout.setNoMoreData(false);
                        if (pageNum - 1 == 1) {
                            if (fansBean.getMap().getCustomerList() != null && fansBean.getMap().getCustomerList().size() != 0) {
                                empty.setVisibility(View.GONE);
                                fansAdapter.clear();
                                fansAdapter.addAll(fansBean.getMap().getCustomerList());
                            } else {
                                empty.setVisibility(View.VISIBLE);
                            }
                        } else {
                            fansAdapter.addAll(fansBean.getMap().getCustomerList());
                        }

                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();
                        fansAdapter.notifyDataSetChanged();
                    } else {
                        if (pageNum - 1 == 1) {
                            empty.setVisibility(View.VISIBLE);
                        } else {
                            empty.setVisibility(View.GONE);
                        }
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(fansBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
                dismissDialog();
            }
        });
    }

    private void requestFollowData() {
        showDialog(this, null);
        HttpParams params = new HttpParams();
        params.put("myId", tokenBean.getId());
        params.put("customerId", id);
        params.put("pageNum", pageNum++);
        params.put("pageSize", pageSize);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_MY_FOLLOWER, params, Url.GET_MY_FOLLOWER, new Callback<FansBean>() {
            @Override
            public void onSuccess(FansBean fansBean) {
                if (fansBean.getResult().equals(Constant.REQUEST_SUCCESS)) {
                    if (fansBean.getMap() != null) {
                        refreshLayout.setNoMoreData(false);
                        if (pageNum - 1 == 1) {
                            if (fansBean.getMap().getCustomerList() != null && fansBean.getMap().getCustomerList().size() != 0) {
                                empty.setVisibility(View.GONE);
                                fansAdapter.clear();
                                fansAdapter.addAll(fansBean.getMap().getCustomerList());
                            } else {
                                empty.setVisibility(View.VISIBLE);
                            }
                        } else {
                            fansAdapter.addAll(fansBean.getMap().getCustomerList());
                        }

                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();
                        fansAdapter.notifyDataSetChanged();
                    } else {
                        if (pageNum - 1 == 1) {
                            empty.setVisibility(View.VISIBLE);
                        } else {
                            empty.setVisibility(View.GONE);
                        }
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(fansBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
                dismissDialog();
            }
        });
    }

    private void resquestData() {
        showDialog(this, null);
        HttpParams params = new HttpParams();
        params.put("customerId", id);
        params.put("pageNum", pageNum++);
        params.put("pageSize", pageSize);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_OTHER_DYNAMIC_LIST, params, Url.GET_OTHER_DYNAMIC_LIST, new Callback<DynamicListBean>() {
            @Override
            public void onSuccess(DynamicListBean dynamicListBean) {
                if (dynamicListBean.getResult().equals(Constant.REQUEST_SUCCESS)) {
                    if (pageNum - 1 == 1) {
                        if (dynamicListBean.getMap().getPageInfo().getList() != null && dynamicListBean.getMap().getPageInfo().getList().size() != 0) {
                            empty.setVisibility(View.GONE);
                            dynamicAdapter.clear();
                            dynamicAdapter.addAll(dynamicListBean.getMap().getPageInfo().getList());
                        } else {
                            empty.setVisibility(View.VISIBLE);
                        }
                    } else {
                        dynamicAdapter.addAll(dynamicListBean.getMap().getPageInfo().getList());
                    }
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    dynamicAdapter.notifyDataSetChanged();

                    int pagecount;
                    if (dynamicListBean.getMap().getPageInfo().getTotal() % pageSize == 0) {
                        pagecount = dynamicListBean.getMap().getPageInfo().getTotal() / pageSize;
                    } else {
                        pagecount = dynamicListBean.getMap().getPageInfo().getTotal() / pageSize + 1;
                    }

                    if (pageNum > pagecount) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.setNoMoreData(false);
                    }
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(dynamicListBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {

        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if ("follow".equals(flag)) {
            requestFollowData();
        } else if ("fans".equals(flag)) {
            requestFasData();
        } else if ("dynamic".equals(flag)) {
            resquestData();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        if ("follow".equals(flag)) {
            requestFollowData();
        } else if ("fans".equals(flag)) {
            requestFasData();
        } else if ("dynamic".equals(flag)) {
            resquestData();
        }
    }
}
